package com.orangelife.mobile.surrounding.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.orangelife.mobile.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.orangelife.mobile.surrounding.activity.SurroundingGoodListActivity;
import com.orangelife.mobile.surrounding.biz.MerchantBiz;
import com.orangelife.mobile.surrounding.entity.MerchantEntity;
import com.thirdsdk.qiniu.QiniuHelper;

import java.util.List;

/**
 * 周边商户列表的适配器
 *
 * @param <T>
 * @author 绯若虚无
 * @version v3.4
 */
public class SurroundingMerchantAdapter<T> extends FatherAdapter<T> {

    private Context mContext;
    private List<T> mList;
    private DisplayImageOptions options;
    @Override
    public BaseAdapter setList(List<T> list) {
        this.mList = list;
        return SurroundingMerchantAdapter.this;
    }

    public SurroundingMerchantAdapter(Context mContext, List<T> list) {
        this.mContext = mContext;
        this.mList = list;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.shop_header)
                .showImageForEmptyUri(R.drawable.shop_header)
                .showImageOnFail(R.drawable.shop_header)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new RoundedBitmapDisplayer(10))
                .build();
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_lv_surrounding_merchant, null);
            holder.ivImage = (ImageView) convertView.findViewById(R.id.ivImage);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
            holder.tvContent1 = (TextView) convertView.findViewById(R.id.tvContent1);
            holder.tvContent2 = (TextView) convertView.findViewById(R.id.tvContent2);
            holder.layItem = (RelativeLayout) convertView.findViewById(R.id.layItem);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        /* listview填数据 */
        MerchantEntity me = (MerchantEntity) mList.get(position);
        holder.tvTitle.setText(me.getMerchantName());
        holder.tvContent1.setText(MerchantBiz.getDispatchInfo(me.getDispatchInfo()));
        holder.tvContent2.setText("营业时间：" + me.getShopHours());
//        ImageDownloader.getImageDownloader().download(me.getMerchantLogo(), holder.ivImage);
        ImageLoader.getInstance().displayImage(QiniuHelper.getUrlWithQiniuListLogo(me.getMerchantLogo()), holder.ivImage,options);
        holder.layItem.setOnClickListener(new listener(me.getMerID(), me.getMerchantName()));
        return convertView;
    }

    class listener implements View.OnClickListener {

        private String merID = null;
        private String merchantName;

        public listener(String merID, String merchantName) {
            this.merID = merID;
            this.merchantName = merchantName;
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.layItem:
                    jump(merID, merchantName);
                    break;
                default:
                    break;
            }

        }
    }

    private void jump(String merID, String merchantName) {
        Intent intent = new Intent();
        intent.putExtra("merID", merID);
        intent.putExtra("merchantName", merchantName);
        intent.setClass(mContext, SurroundingGoodListActivity.class);
        mContext.startActivity(intent);
    }

    class ViewHolder {
        /** 照片 */
        private ImageView ivImage;
        /** 商品名称 */
        private TextView tvTitle;
        /** 免配送费，格式：满￥X元免配送费 */
        private TextView tvContent1;
        /** 营业时间，格式：营业时间：10:00-19:00 */
        private TextView tvContent2;
        /** 点击事件 */
        private RelativeLayout layItem;
    }
}
